package DoExercise.HSP_ZCY.A009_图结构.A001_并查集;

public class GraphGenerator
{
    /**
     *
     * 将题目给的图转化为自己理解的图结构
     *
     * @param matrix
     * @return
     */
    public static Graph createGraph(Integer[][] matrix)
    {
        Graph graph = new Graph();
        for (int i = 0; i < matrix.length; i++)
        {
            Integer from = matrix[i][0];
            Integer to = matrix[i][1];
            Integer weight = matrix[i][2];
            //建立点集
            if (!graph.nodes.containsKey(from))
            {
                graph.nodes.put(from, new Node(from));
            }
            if (!graph.nodes.containsKey(to))
            {
                graph.nodes.put(to, new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            //建立边集
            Edge newEdge = new Edge(weight, fromNode, toNode);
            //点集建立联系
            fromNode.nexts.add(toNode);
            fromNode.out++;
            toNode.in++;
            //边集建立联系
            fromNode.edges.add(newEdge);
            //将边加入到图集合中
            graph.edges.add(newEdge);
        }
        return graph;
    }
    
}
